package com.icez.loadlayoutlibrary

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
/**
 * 加载布局，子布局必须只能有一个
 * @sample
 * @author Icez
 */
class LoadLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    // 错误布局
    var mErrorLayoutId: Int = NO_ID
    // 空布局
    var mEmptyLayoutId: Int = NO_ID
    // 加载中布局
    var mLoadingLayoutId: Int = NO_ID

    var mLayoutInflater: LayoutInflater? = null

    // 布局显示监听
    var mOnShowLayoutListener:OnShowLayoutListener ?= null

    companion object{
        // 错误布局，请再application中配置，优先级最低
        var mStaticErrorLayoutId:Int = NO_ID
        // 空布局，请再application中配置，优先级最低
        var mStaticEmptyLayoutId:Int = NO_ID
        // 加载中布局，请再application中配置，优先级最低
        var mStaticLoadingLayoutId:Int = NO_ID
    }

    init {
        mLayoutInflater = LayoutInflater.from(context)
        val typedArray = context.obtainStyledAttributes(attrs,R.styleable.LoadLayout)
        mErrorLayoutId = typedArray.getResourceId(R.styleable.LoadLayout_errorLayoutId,
            NO_ID)
        mEmptyLayoutId = typedArray.getResourceId(R.styleable.LoadLayout_emptyLayoutId, NO_ID)
        mLoadingLayoutId = typedArray.getResourceId(R.styleable.LoadLayout_loadingLayoutId,
            NO_ID)

        typedArray.recycle()

        if(mErrorLayoutId == NO_ID){
            mErrorLayoutId = mStaticErrorLayoutId
        }
        if(mEmptyLayoutId == NO_ID){
            mEmptyLayoutId = mStaticEmptyLayoutId
        }
        if(mLoadingLayoutId == NO_ID){
            mLoadingLayoutId = mStaticLoadingLayoutId
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        if (childCount == 0) {
            return
        }
        if (childCount > 1) {
            removeViews(1, childCount - 1)
        }
    }

    /**
     * 显示失败布局
     * @sample
     * @author Icez
     */
    fun showErrorLayout() {
        val mErrorBinding = mLayoutInflater?.let {
            DataBindingUtil.inflate<ViewDataBinding>(
                it,
                mErrorLayoutId,
                this,
                false
            )
        }
        if (childCount == 0) {
            return
        }
        addView(mErrorBinding?.root)
        if (childCount > 2) {
            removeViews(1, childCount - 2)
        }

        mOnShowLayoutListener?.showErrorLayout(mErrorBinding)
    }

    /**
     * 显示空布局
     * @sample
     * @author Icez
     */
    fun showEmptyLayout() {
        val mEmptyBinding = mLayoutInflater?.let {
            DataBindingUtil.inflate<ViewDataBinding>(
                it,
                mEmptyLayoutId,
                this,
                false
            )
        }
        if (childCount == 0) {
            return
        }
        addView(mEmptyBinding?.root)
        if (childCount > 2) {
            removeViews(1, childCount - 2)
        }

        mOnShowLayoutListener?.showEmptyLayout(mEmptyBinding)
    }

    /**
     * 显示加载中布局
     * @sample
     * @author Icez
     */
    fun showLoadingLayout() {
        if(mLoadingLayoutId == NO_ID){
            return
        }
        val mLoadingBinding = mLayoutInflater?.let {
            DataBindingUtil.inflate<ViewDataBinding>(
                it,
                mLoadingLayoutId,
                this,
                false
            )
        }
        if (childCount == 0) {
            return
        }
        addView(mLoadingBinding?.root)
        if (childCount > 2) {
            removeViews(1, childCount - 2)
        }

        mOnShowLayoutListener?.showLoadingLayout(mLoadingBinding)
    }

    /**
     * 显示自定义显示布局
     * @param customLayoutId 布局id
     * @sample
     * @author Icez
     */
    fun showCustomLayout(
        customLayoutId: Int
    ) {
        val mCustomBinding = mLayoutInflater?.let {
            DataBindingUtil.inflate<ViewDataBinding>(
                it,
                customLayoutId,
                this,
                false
            )
        }
        if (childCount == 0) {
            return
        }
        addView(mCustomBinding?.root)
        if (childCount > 2) {
            removeViews(1, childCount - 2)
        }

        mOnShowLayoutListener?.showCustomLayout(mCustomBinding)
    }

    /**
     * 显示内容
     * @sample
     * @author Icez
     */
    fun showContentLayout() {
        if (childCount == 0) {
            return
        }
        if (childCount > 1) {
            removeViews(1, childCount - 1)
        }
    }

    /**
     * 设置布局显示监听
     * @sample
     * @author Icez
     */
    fun setOnShowLayoutListener(onShowLayoutListener:OnShowLayoutListener){
        mOnShowLayoutListener = onShowLayoutListener
    }

    interface OnShowLayoutListener{
        fun showLoadingLayout(mViewDataBinding: ViewDataBinding?)
        fun showEmptyLayout(mViewDataBinding: ViewDataBinding?)
        fun showErrorLayout(mViewDataBinding: ViewDataBinding?)
        fun showCustomLayout(mViewDataBinding: ViewDataBinding?)
    }
}